var BSTYUN = BSTYUN || {};
BSTYUN.appcenter = {
	define: {
		keyword: '\u5173\u952E\u5B57' // 字符：关键字
	},
	init: function(){
		var that = this;
		/* 关键字显隐 */
		that.inputFocusBlur();
		/* 搜索更新dom */
		that.search();
		/*查询应用中心数据*/
		var datas = {
			page : 1,
			industry: null,
			category: null,
			goodsName: null
		}
		that.searchData(datas);
	},
	inputFocusBlur: function(){
		var that = this,
			keyword = that.define.keyword,
			$box = $(".search-box"),
			$input = $("#search-txt");
		$input.on("focus",function(){
			var $this = $(this),
				txt = $.trim( $this.val() );
			if( txt == keyword ){
				$this.val('');
			}
		}).on("blur",function(){
			var $this = $(this),
				txt = $.trim( $this.val() );
			if( txt == ''){
				$this.val( keyword );
			}
		});
	},
	/* 搜索查询 */
	search: function(){
		var that = this,
			define_keyword = that.define.keyword,
			$btn = $(".search-btn"),
			$industry = $("#industry"),
			$category = $("#product_category"),
			$keyword = $("#search-txt"),
			$page = $("#currPage"),
			$probox = $("#probox");
		function getData(){
			var value = $.trim( $keyword.val() ),
				keyword = value == define_keyword ? '' :  value;
			return {
				industry: $.trim( $industry.val() ),
				category: $.trim( $category.val() ),
				goodsName: keyword ,
				page: $.trim( $page.val() )
			};
		};
		function sendRequest( data ){
			if( !!!data) return;
			BSTYUN.appcenter.searchData(data);
		};
		$btn.on("click",function(){
			var data = getData();
			sendRequest( data );
		});
		$page.on("change",function(){
			var data = getData();
			sendRequest( data );
		});
	},
	/*查询应用中心数据*/
	searchData : function(data) {
		$.ajax({
			url: '/goods/applyCenter/goodsList',
			type: 'post',
			dataType: 'json',
			data: data,
			success: function( data ){
				$('#count').html(data.totalRows);
				$("#currPage").val(data.currPage);
				
				$('#probox').html('');
				var content = '';
				$.each(data.goodsList,function(index,goods){
					//跳转购买路径
					var url = "javascript:window.open('/mall')";
					if(data.goodsList.length > 0) {
						content += '<li class="prolist cle">' +
					       '<div class="proimg">' + 
					       '<a href="'+url+'"><img src="'+goods.goodsImgUrl+'"/></a></div>' +
					       '<div class="prodetail">' +
					       '<dl><dt>' +
					       '<a href="'+url+'">'+goods.goodsName+'</a></dt>' +
					       '<dd class="red">报价：￥'+goods.goodsPrice+goods.goodsPriceUnitName+'</dd>' +
					       '<dd>'+goods.goodsCharacter+'</dd></dl></div>';
						
						if(goods.typeNum == 0) {
							content += '<a href="'+url+'" class="gotobuy">购买</a></li>';
						}else {
							content += '<a href="javascript:void(0)" class="gotobuy">已购买</a></li>';
						}
					}
					$('#probox').html(content);
				})
			},
			error: function(){
				BSTYUN.dialog.warn("网络错误，请稍后重试！！");
			}
		});
	}
};
$(function(){
	BSTYUN.appcenter.init();
});